package com.srwl.mytx.im;

import android.util.Log;

import com.srwl.mytx.domain.Message;
import com.srwl.mytx.im.interf.MessageSendListener;
import com.srwl.mytx.im.protobuf.MessageProtobuf;


/**
 * <p>@ProjectName:     NettyChat</p>
 * <p>@ClassName:       MessageProcessor.java</p>
 * <p>@PackageName:     com.freddy.chat.im</p>
 * <b>
 * <p>@Description:     消息处理器</p>
 * </b>
 * <p>@author:          FreddyChen</p>
 * <p>@date:            2019/04/10 03:27</p>
 * <p>@email:           chenshichao@outlook.com</p>
 */
public class MessageProcessor {

    private static final String TAG = MessageProcessor.class.getSimpleName();

    private MessageProcessor() {

    }

    private static class MessageProcessorInstance {
        private static final MessageProcessor INSTANCE = new MessageProcessor();
    }

    public static MessageProcessor getInstance() {
        return MessageProcessorInstance.INSTANCE;
    }

    /**
     * 接收消息
     *
     * @param message
     */

    public void receiveMsg(final Message message) {
        CThreadPoolExecutor.runInBackground(new Runnable() {

            @Override
            public void run() {
//                try {
//                    IMessageHandler messageHandler = MessageHandlerFactory.getHandlerByMsgType(message.getHead().getMsgType());
//                    if (messageHandler != null) {
//                        messageHandler.execute(message);
//                    } else {
//                        Log.e(TAG, "未找到消息处理handler，msgType=" + message.getHead().getMsgType());
//                    }
//                } catch (Exception e) {
//                    Log.e(TAG, "消息处理出错，reason=" + e.getMessage());
//                }
            }
        });
    }

    /**
     * 发送消息
     *
     * @param message
     */

    public void sendMsg(final Message message) {
        CThreadPoolExecutor.runInBackground(new Runnable() {

            @Override
            public void run() {

                MessageProtobuf.Msg.Builder msgBuilder = MessageBuilder.getProtoBufMessageBuilderByAppMessage(message, message.getType());
                IMSClientBootstrap.getInstance().sendMessage(msgBuilder.build());

            }
        });
    }

    public void sendMsg(final Message message, int messageType) {
        CThreadPoolExecutor.runInBackground(new Runnable() {

            @Override
            public void run() {

                MessageProtobuf.Msg.Builder msgBuilder = MessageBuilder.getProtoBufMessageBuilderByAppMessage(message, messageType);
                IMSClientBootstrap.getInstance().sendMessage(msgBuilder.build());

            }
        });
    }

    public void sendRealTimeMessage(final Message message, MessageSendListener listener) {
        CThreadPoolExecutor.runInBackground(new Runnable() {

            @Override
            public void run() {
                boolean isActive = IMSClientBootstrap.getInstance().isActive();
                if (isActive) {
                    MessageProtobuf.Msg.Builder msgBuilder = MessageBuilder.getProtoBufMessageBuilderByAppMessage(message, message.getType());

                    IMSClientBootstrap.getInstance().sendRealTimeMessage(msgBuilder.build(), listener);
                } else {
                    Log.e(TAG, "发送消息失败");
                }
            }
        });
    }

}
